package UI;

import action.UserAction;
import entity.User;
import util.MysqlUtil;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/*
 * 每次添加完新组件以后一定要加上this.getContentPane().add(组件名)!!*/
public class RegisterUI extends JFrame implements MouseListener {
    /*
     * 添加组件:
     * 按钮(随机昵称,注册,返回登录)
     * 标签(用户名,密码,确认密码)
     * 文本框和密码框(username,password,checkPassword)*/
    JButton jb1 = new JButton("注册");
    JButton jb2 = new JButton("返回登录");
    JButton jb3 = new JButton("随机昵称");
    JLabel userNameLabel, passWordLabel, checkPasswordLabel;

    static JTextField userNameText;
    static JPasswordField passWordText;
    static JPasswordField checkPasswordText;


    public RegisterUI() {
        WholeUI wholeUI = new WholeUI(this) {
            @Override
            protected void elseWindow(JFrame jFrame) {
                //设置尺寸
                jFrame.setSize(500, 500);
                //设置标题
                jFrame.setTitle("注册界面");
                //设置界面居中
                jFrame.setAlwaysOnTop(true);


            }

        };
        /*
         * 加载组件按钮,标签,文本框和密码框的方法*/
        loadWindow(this);

    }


    /*
     * 加载组件
     * */

    public void loadWindow(JFrame jFrame) {
        //设置布局
        jFrame.setLayout(null);
        //设置标签文本
        userNameLabel = new JLabel("用户名:");
        passWordLabel = new JLabel("密码:");
        checkPasswordLabel = new JLabel("确认密码:");
        //设置组件文本
        userNameText = new JTextField(20);
        passWordText = new JPasswordField(20);
        checkPasswordText = new JPasswordField(20);
        //设置组件尺寸
        jb1.setBounds(300, 200, 100, 30);
        jb2.setBounds(200, 200, 100, 30);
        jb3.setBounds(100, 200, 100, 30);
        userNameLabel.setBounds(50, 50, 100, 30);
        passWordLabel.setBounds(50, 100, 100, 30);
        checkPasswordLabel.setBounds(50, 150, 100, 30);
        userNameText.setBounds(100, 50, 100, 30);
        passWordText.setBounds(100, 100, 100, 30);
        checkPasswordText.setBounds(100, 150, 100, 30);

        //添加进入容器
        this.getContentPane().add(jb1);
        this.getContentPane().add(jb2);
        this.getContentPane().add(jb3);
        this.getContentPane().add(userNameLabel);
        this.getContentPane().add(passWordLabel);
        this.getContentPane().add(checkPasswordLabel);
        this.getContentPane().add(userNameText);
        this.getContentPane().add(passWordText);
        this.getContentPane().add(checkPasswordText);

        //绑定事件
        jb1.addMouseListener(this);
        jb2.addMouseListener(this);
        jb3.addMouseListener(this);

    }

    /**
     * (可乐盒子)
     * io流
     * 随机昵称的逻辑, 返回 形容词+的+名词
     * 在RandomName文件中(里边内容请自由发挥,越多越好)
     * 随机读一行名词和形容词
     * 并拼接成
     * 形容词+"的"+名词
     */
    public String randomName() {
        String adjPath = "documents/RandomName/形容词.txt";
        String nPath = "documents/RandomName/名词.txt";
        Scanner adjectiveScanner = null;
        Scanner nounScanner = null;
        try {
            adjectiveScanner = new Scanner(new File(adjPath));
            nounScanner = new Scanner(new File(nPath));
            // 读取所有行到集合中
            List<String> adjectives = new ArrayList<>();
            List<String> nouns = new ArrayList<>();
            while (adjectiveScanner.hasNextLine()) {
                adjectives.add(adjectiveScanner.nextLine());
            }
            while (nounScanner.hasNextLine()) {
                nouns.add(nounScanner.nextLine());
            }
            // 随机选择一个形容词和一个名词
            String adjective = adjectives.get(new Random().nextInt(adjectives.size()));
            String noun = nouns.get(new Random().nextInt(nouns.size()));
            // 拼接成昵称
            return adjective + "的" + noun;
        } catch (FileNotFoundException e) {
            System.out.println("文件未找到: " + e.getMessage());
            return null;
        } finally {
            // 关闭扫描器
            if (adjectiveScanner != null) {
                adjectiveScanner.close();
            }
            if (nounScanner != null) {
                nounScanner.close();
            }
        }
    }


    @Override
    public void mouseClicked(MouseEvent e) {
        /*
         * 根据不同按钮绑定不同事件*/
        if (e.getSource() == jb1) {
            /*
             * 获取文本框中的文本,并判断是否为空*/
            String userNameText = RegisterUI.userNameText.getText();
            String passWordText = RegisterUI.passWordText.getText();
            String checkPasswordText = RegisterUI.checkPasswordText.getText();

            //创建消息框判断是否为空或密码是否一致或用户名是否已被注册
            JFrame jFrame1 = new JFrame();
            //消息置顶
            jFrame1.setAlwaysOnTop(true);
            if (userNameText.isEmpty() || passWordText.isEmpty() || checkPasswordText.isEmpty()) {
                JOptionPane.showMessageDialog(jFrame1, "请输入完整信息");
            } else if (!passWordText.equals(checkPasswordText)) {
                JOptionPane.showMessageDialog(jFrame1, "两次密码不一致");
            } else {
                try {
                    User user = new User(userNameText, passWordText);
                    if (MysqlUtil.QueryUser(user)) {
                        JOptionPane.showMessageDialog(jFrame1, "该用户名已被注册");
                    } else {//将用户信息添加到数据库
                        UserAction.register(user);
                        this.dispose();
                        //弹出注册成功界面
                        JOptionPane.showMessageDialog(jFrame1, "注册成功");
                        //创建文件
                        try {
                            //好友列表
                            File file = new File("documents/FriendList/" + LoginUI.userNameText.getText() + ".txt");
                            file.createNewFile();
                            //个人信息(PersonalInformation/用户名的个人简介.txt)
                            File file1 = new File("documents/PersonalInformation/" + LoginUI.userNameText.getText() + "的个人介绍.txt");
                            file1.createNewFile();
                        } catch (IOException ex) {
                            System.out.println("文件创建失败");
                            throw new RuntimeException(ex);
                        }
                        //进入登录界面
                        LoginUI loginUI = new LoginUI();
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        } else if (e.getSource() == jb2) {
            //进入登录界面并关闭注册界面
            LoginUI loginUI = new LoginUI();
            this.dispose();

        } else if (e.getSource() == jb3) {
            //随机昵称
            userNameText.setText(randomName());
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

